import type { User } from "./user.d";
import { defineStore } from "pinia";
import { ref } from "vue";
import { login, getUserInfo } from "@/api/system";
export const useUserStore = defineStore("user", ()=>{
  
    //登录状态token
    const token = ref<string>("");
     //设置token
     const setToken = (newToken: string) => {
      token.value = newToken;
    };
    //获取token
    const getToken = () => {
      return token.value;
    };
    
    // 用户信息
    const userInfo = ref<User>();

    //设置用户信息
    const setUserInfo = (user: User) => {
      userInfo.value = user;
    };

    //获取用户信息
    const getUserInfo = () => {
      return userInfo.value;
    };

    //登录接口
    const handlerLogin = async (data: any) => {
      const res = await login(data);
      if(res&&res.data&&res.data.token){
        setToken(res.data.token);
        getUserInfoApi();
      }else{
        setToken("");
      }
    };
    //请求用户信息接口调用
    const getUserInfoApi = async () => {
      const res = await getUserInfo();
    };
    //返回变量和方法
    return { userInfo,setUserInfo,getUserInfo,handlerLogin ,getToken};
},{
    persist: true
});